package org.example.promptword.common;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * 统一响应消息报文
 *
 * @param <T> T对象
 * @author caiqiang
 */
@Data
@ApiModel(value = "统一响应消息报文")
public class ResponseInfo<T> {

    @ApiModelProperty(value = "状态码", required = true)
    private String code;

    @ApiModelProperty(value = "消息内容", required = true)
    private String message;

    @ApiModelProperty(value = "控制台打印日志")
    private String log;

    /**
     * 服务器IP
     */
    @ApiModelProperty(value = "服务器IP", name = "data", position = 3)
    private String timestamp = DateTimeUtil.standardFormat(new Date());

    @ApiModelProperty(value = "业务数据")
    @JsonInclude(JsonInclude.Include.ALWAYS)
    @JsonSerialize()
    private T data;

    // 添加无参构造函数
    public ResponseInfo() {
    }

    private ResponseInfo(ResponseEnum responseEnum) {
        this(responseEnum.getCode(), responseEnum.getMessage());
    }

    private ResponseInfo(T data) {
        this(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), data);
    }

    private ResponseInfo(String code, String message) {
        this(code, message, null);
    }

    private ResponseInfo(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public ResponseInfo(String code, String message, String log, T data) {
        this.code = code;
        this.message = message;
        this.log = log;
        this.data = data;
    }

    /**
     * 返回状态码
     *
     * @param <T> 泛型标识
     * @return ApiResult
     */
    public static <T> ResponseInfo<T> success() {
        return new ResponseInfo<>(ResponseEnum.SUCCESS);
    }


    public static <T> ResponseInfo<T> success(T data) {
        return new ResponseInfo<>(data);
    }

    public static <T> ResponseInfo<T> success(T data, String log) {
        return new ResponseInfo<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), log, data);
    }


    public static <T> ResponseInfo<T> fail(ResponseEnum responseEnum) {
        return new ResponseInfo<>(responseEnum);
    }

    public static <T> ResponseInfo<T> fail(String code, String message) {
        return new ResponseInfo<>(code, message);
    }

    public static <T> ResponseInfo<T> fail(String code, String message, String log) {
        return new ResponseInfo<>(code, message, log, null);
    }

    public static <T> ResponseInfo<T> fail(ResponseEnum responseEnum, String message) {
        return new ResponseInfo<>(responseEnum.getCode(), message);
    }

    public static <T> ResponseInfo<T> fail(ResponseEnum responseEnum, String message, String log) {
        return new ResponseInfo<>(responseEnum.getCode(), message, log, null);
    }


}
